import { bankInfo } from '@/gql/user';
import { toPublish } from '@mqtt';
import { defineStore } from 'pinia';
import { getDriverID } from './driverID';

export const useFinanceStore = defineStore('finance', {
  state: () => ({
    bankID: '',
    real_name: '',
    bank_name: '',
    bank_info: '',
    card_no: '',
  }),
  getters: {
    bankInfo(state) {
      return {
        id: state.bankID,
        real_name: state.real_name,
        bank_name: state.bank_name,
        bank_info: state.bank_info,
        card_no: state.card_no,
      };
    },
  },
  actions: {
    getBankInfo() {
      const payload = {
        query: bankInfo,
        variables: { driver_id: getDriverID() },
      };
      toPublish('ql/driver/getBankInfo', payload, (obj: any) => {
        const { bank } = obj.data;
        this.bankID = bank.id || '';
        this.real_name = bank.real_name || '';
        this.bank_name = bank.bank_name || '';
        this.bank_info = bank.bank_info || '';
        this.card_no = bank.card_no || '';
      });
    },
  },
});
